package com.example.jetpackcomposelearning


import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MaterialTheme{
                FirstPage()
            }
        }
        initPermission()
    }

    private fun initPermission() {
        val permissions = arrayOf(
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.INTERNET,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_MEDIA_IMAGES,
            Manifest.permission.READ_MEDIA_VIDEO
        )

        val toApplyList = java.util.ArrayList<String>()

        for (perm in permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(
                    this,
                    perm
                )
            ) {
                toApplyList.add(perm)
            }
        }
        val tmpList = arrayOfNulls<String>(toApplyList.size)
        if (toApplyList.isNotEmpty()) {
            ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123)
        }
    }
}




@Composable
fun FirstPage() {
    val context = LocalContext.current

    Box{
        Box(modifier = Modifier.fillMaxSize()){
            Image(
                painter = painterResource(id = R.drawable.sidp),
                modifier = Modifier.fillMaxSize(),
                contentScale = ContentScale.FillHeight,
                contentDescription = null
            )
        }
        Box(
            modifier = Modifier.fillMaxSize()) {
            TextButton(
                onClick = {
                    val intent = Intent()
                    intent.setClass(context, SceneCreationActivity::class.java)
                    context.startActivity(intent)
                },
                modifier = Modifier
                    .wrapContentSize(align = Alignment.Center, unbounded = false)
                    .align(alignment = Alignment.TopStart)
                    .padding(horizontal = 40.dp, vertical = 30.dp),
                colors = ButtonDefaults.buttonColors(containerColor = colorResource(id = R.color.black)),
                shape = RectangleShape

            ){
                Text(
                    "情景创作",
                    fontSize = 30.sp,
                    fontWeight = FontWeight(1),
                    color = colorResource(id = R.color.white))
            }

        }
    }

}


@Preview(device = Devices.DESKTOP)
@Composable
fun PreviewFirstPage() {
    MaterialTheme {
        FirstPage()
    }
}


